﻿package mortal.game.view.effect
{
    import com.gengine.core.frame.*;
    import com.gengine.utils.pools.*;
    import flash.display.*;
    import flash.filters.*;
    import flash.utils.*;

    public class GlowFilterEffect extends Object
    {
        private var _targetDic:Dictionary;
        private var _targetCallBack:Dictionary;
        private var _frame:FrameTimer;
        private var _effectNum:int;
        private var _tempFilters:Array;

        public function GlowFilterEffect()
        {
            this._targetDic = new Dictionary();
            this._targetCallBack = new Dictionary();
            this._effectNum = 0;
            this._frame = new FrameTimer();
            this._frame.addListener(TimerType.ENTERFRAME, this.onEnterFrameHandler);
            return;
        }// end function

        private function onEnterFrameHandler(param1:FrameTimer) : void
        {
            var _loc_2:* = undefined;
            var _loc_3:Object = null;
            var _loc_4:Array = null;
            var _loc_5:GlowFilter = null;
            for (_loc_2 in this._targetDic)
            {
                
                if (_loc_2 == "null")
                {
                    continue;
                }
                _loc_3 = this._targetDic[_loc_2];
                if (_loc_3["repeat"] != 0)
                {
                    if (_loc_3["count"] >= _loc_3["repeat"])
                    {
                        if (this._targetCallBack[_loc_2])
                        {
                            (this._targetCallBack[_loc_2] as Function).call();
                        }
                        this.unRegist(_loc_2);
                        continue;
                    }
                }
                if (_loc_3.hasOwnProperty("filters"))
                {
                    _loc_4 = _loc_3["filters"];
                    for each (_loc_5 in _loc_4)
                    {
                        
                        if (_loc_5.blurX <= _loc_3["min"])
                        {
                            _loc_5.blurX = _loc_3["min"];
                            _loc_5.blurY = _loc_3["min"];
                            if (_loc_3["step"] < 0)
                            {
                                _loc_3["step"] = _loc_3["step"] * -1;
                            }
                            _loc_3["count"] = _loc_3["count"] + 0.5;
                        }
                        else if (_loc_5.blurX >= _loc_3["max"])
                        {
                            _loc_5.blurX = _loc_3["max"];
                            _loc_5.blurY = _loc_3["max"];
                            if (_loc_3["step"] > 0)
                            {
                                _loc_3["step"] = _loc_3["step"] * -1;
                            }
                            _loc_3["count"] = _loc_3["count"] + 0.5;
                        }
                        _loc_5.blurX = _loc_5.blurX + _loc_3["step"];
                        _loc_5.blurY = _loc_5.blurY + _loc_3["step"];
                    }
                }
                if (_loc_2 && _loc_2 != "null" && _loc_4)
                {
                    _loc_2.filters = _loc_4;
                }
            }
            return;
        }// end function

        public function regist(param1:DisplayObject, param2:Array, param3:Number = 1, param4:int = 20, param5:int = 3, param6:int = 0, param7:Function = null) : void
        {
            var _loc_8:GlowFilter = null;
            var _loc_9:GlowFilter = null;
            if (param1 == null || param2 == null)
            {
                return;
            }
            if (this._targetDic[param1] != null)
            {
                this.disposeTarget(param1);
                var _loc_10:String = this;
                var _loc_11:* = this._effectNum - 1;
                _loc_10._effectNum = _loc_11;
            }
            this._tempFilters = [];
            for each (_loc_9 in param2)
            {
                
                _loc_8 = ObjectPool.getObject(GlowFilter);
                _loc_8.color = _loc_9.color;
                _loc_8.alpha = _loc_9.alpha;
                _loc_8.blurX = _loc_9.blurX;
                _loc_8.blurY = _loc_9.blurY;
                _loc_8.strength = _loc_9.strength;
                _loc_8.quality = _loc_9.quality;
                _loc_8.inner = _loc_9.inner;
                _loc_8.knockout = _loc_9.knockout;
                this._tempFilters.push(_loc_8);
            }
            param2.splice();
            this._targetDic[param1] = {filters:this._tempFilters, step:param3, max:param4, min:param5, repeat:param6, count:0};
            if (param7 != null)
            {
                this._targetCallBack[param1] = param7;
            }
            var _loc_10:String = this;
            var _loc_11:* = this._effectNum + 1;
            _loc_10._effectNum = _loc_11;
            if (!this._frame.running)
            {
                this._frame.start();
            }
            return;
        }// end function

        public function getFilters(param1:DisplayObject) : Array
        {
            if (this._targetDic[param1] != null)
            {
                return this._targetDic[param1]["filters"];
            }
            return null;
        }// end function

        public function unRegist(param1:DisplayObject) : void
        {
            if (this._targetDic[param1] != null)
            {
                this.disposeTarget(param1);
                var _loc_2:String = this;
                var _loc_3:* = this._effectNum - 1;
                _loc_2._effectNum = _loc_3;
            }
            else
            {
                return;
            }
            if (this._effectNum <= 0)
            {
                if (this._frame.running)
                {
                    this._frame.stop();
                }
            }
            return;
        }// end function

        private function disposeTarget(param1:DisplayObject) : void
        {
            var _loc_3:Array = null;
            var _loc_4:GlowFilter = null;
            var _loc_2:* = this._targetDic[param1];
            if (_loc_2 && _loc_2.hasOwnProperty("filters"))
            {
                _loc_3 = _loc_2["filters"];
                for each (_loc_4 in _loc_3)
                {
                    
                    ObjectPool.disposeObject(_loc_4, GlowFilter);
                }
            }
            delete this._targetDic[param1];
            if (this._targetCallBack[param1])
            {
                delete this._targetCallBack[param1];
            }
            param1.filters = null;
            return;
        }// end function

        public function unRegistAll() : void
        {
            var _loc_1:* = undefined;
            for (_loc_1 in this._targetDic)
            {
                
                this.disposeTarget(_loc_1 as DisplayObject);
            }
            this._effectNum = 0;
            if (this._frame.running)
            {
                this._frame.stop();
            }
            return;
        }// end function

    }
}
